package com.common.www.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.swagger.annotations.Api;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    /**
     * 通过 createRestApi函数来构建一个DocketBean
     * 函数名,可以随意命名,喜欢什么命名就什么命名
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())//调用apiInfo方法,创建一个ApiInfo实例,里面是展示在文档页面信息内容
                .groupName("A全部")
                .select()
                //控制暴露出去的路径下的实例
                //如果某个接口不想暴露,可以使用以下注解
                //@ApiIgnore 这样,该接口就不会暴露在 swagger2 的页面下
                //默认扫描全部
                .apis(RequestHandlerSelectors.basePackage(""))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean()
    public Docket controller1Apis() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("授权")
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.regex(".*/auth/.*"))
                .build()
                .apiInfo(apiInfo())
                .enable(true);
    }

    @Bean()
    public Docket controllerFileApis() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("文件")
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.regex(".*/file/.*"))
                .build()
                .apiInfo(apiInfo())
                .enable(true);
    }


    @Bean()
    public Docket controllerStatisticsApis() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("统计")
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.regex(".*/statistics/.*"))
                .build()
                .apiInfo(apiInfo())
                .enable(true);
    }

    @Bean()
    public Docket controllerMedical() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("医疗")
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.regex(".*/medical/.*"))
                .build()
                .apiInfo(apiInfo())
                .enable(true);
    }

    //构建 api文档的详细信息函数
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("Swagger2 ")
                //条款地址
                .termsOfServiceUrl("http://despairyoke.github.io/")
                .contact(new Contact("淞哥", "blog.csdn.net", "luozisong@126.com"))
                .version("1.0")
                //描述
                .description("API 描述")
                .build();
    }
}